package com.monster.starter.exception.filter;

import com.monster.starter.exception.service.MonsterExceptionService;
import jakarta.servlet.FilterChain;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.filter.OncePerRequestFilter;
/**
 * 全区异常拦截处理，spring 的无法处理过滤器的
 * @author kuang
 */
@Component
@Order(Integer.MIN_VALUE + 10)
public class MonsterExceptionFilter extends OncePerRequestFilter {
    /**
     * 具体异常处理
     */
    @Autowired
    private MonsterExceptionService exceptionService;

    /**
     * 异常拦截处理
     * @param request 请求对象
     * @param response 响应对象
     * @param filterChain 拦截链
     */
    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) {
        try {
            // 执行过滤器链路
            filterChain.doFilter(request, response);
        } catch (Throwable e) {
            // 异常处理
            exceptionService.exceptionHandler(request,response, e);
        }
    }
}
